package com.educate.book.rpc.provide.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.educate.book.dao.BookDao;
import com.educate.book.dao.BookPageDao;
import com.educate.book.model.bo.BookPageBO;
import com.educate.book.model.po.Book;
import com.educate.book.model.po.BookPage;
import com.educate.book.rpc.api.BookPageService;
import com.educate.common.utils.converter.ListUtils;
import com.educate.common.utils.exception.AppException;
import com.educate.common.web.BeanUtils;
import com.educate.common.web.IdGen;

import lombok.extern.log4j.Log4j2;

@Log4j2
@Service(
        version = "${api.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class BookPageServiceImpl implements BookPageService {

	@Resource BookPageDao bookPageDao;
	@Resource
	BookDao bookDao;

    @Override
    public String create(BookPageBO bo) {
    	bo.setBookPageId(IdGen.uuid());
    	BookPage bp = bookPageDao.insert(bo);
    	//加入书本页码列表
		Optional<Book> optional = bookDao.findById(bo.getBookId());
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        Book book =   optional.get();
	    if(book.getPageList()!=null) {
	    	List<BookPage> bpList = book.getPageList();
	    	bpList.add(bp);
	    	bookDao.save(book);
	    }
	  JSONObject json = new JSONObject();
	  json.put("id", bo.getBookPageId());
      return json.toJSONString();
    }

    @Override
    public void delete(BookPageBO bo) {
    	//处理书本页码列表
		Optional<Book> optional = bookDao.findById(bo.getBookId());
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        Book book =   optional.get();
	    if(book.getPageList()!=null) {
	    	List<BookPage> bpList = book.getPageList();
	    	Optional<BookPage> optional2 = bookPageDao.findById(bo.getBookPageId());
	         if (!optional2.isPresent()) {
	             throw new AppException("不存在的实例");
	         }
	        BookPage bp = optional2.get();
	        bpList.remove(bp);
	        List<BookPage> bpList2 = bpList;
	        book.setPageList(bpList2);
	    	bookDao.save(book);
	    }
        bookPageDao.delete(bo);
        
    }

    @Override
    public void delete(String bookPageId) {
    	//处理书本页码列表
    	Optional<BookPage> optional2 = bookPageDao.findById(bookPageId);
        if (!optional2.isPresent()) {
            throw new AppException("不存在的实例");
        }
        BookPage bp = optional2.get();
		Optional<Book> optional = bookDao.findById(bp.getBookId());
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        Book book =   optional.get();
	    if(book.getPageList()!=null) {
	    	List<BookPage> bpList = book.getPageList();
	        bpList.remove(bp);
	        List<BookPage> bpList2 = bpList;
	        book.setPageList(bpList2);
	    	bookDao.save(book);
	    }
        bookPageDao.deleteById(bookPageId);
    }

    @Override
    public BookPageBO detail(String bookPageId) {
        Optional<BookPage> optional = bookPageDao.findById(bookPageId);
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        return new BookPageBO(optional.get());
    }

    @Override
    public BookPageBO detail(BookPageBO bo) {
        return detail(bo.getBookPageId());
//        return null;
    }

    @Override
    public void update(BookPageBO bo) {
    	 Optional<BookPage> optional = bookPageDao.findById(bo.getBookPageId());
         if (!optional.isPresent()) {
             throw new AppException("不存在的实例");
         }
         BeanUtils.copyPropertiesNonEmpty(bo, optional.get());
         bookPageDao.save(optional.get());
    	
    	//更新绘本目录 
		Optional<Book> optional2 = bookDao.findById(bo.getBookId());
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        Book book = optional2.get();
        BookPage updateBP = null;
	    if(book.getPageList()!=null) {
	    	List<BookPage> bpList = book.getPageList();
	    	int i = 0;
	    	for (; i < bpList.size(); i++) {
				BookPage bp = bpList.get(i);
				if(bp.getBookPageId().equals(bo.getBookPageId())){
					updateBP = bp;
					break;
				}
			}
	    	BeanUtils.copyProperties(bo, updateBP);
	        bpList.remove(i);
	        bpList.add(updateBP);
	        List<BookPage> bpList2 = bpList;
	        book.setPageList(bpList2);
	    	bookDao.save(book);
	    }
       
    }

    @Override
    public Page<BookPageBO> query(BookPageBO bo, PageRequest pageRequest) {
        return bookPageDao.query(bo, pageRequest).map(BookPageBO::new);
    }

    @Override
    public List<BookPageBO> list(BookPageBO bo) {
        List<BookPage> result = bookPageDao.findAll();
        return ListUtils.map(result, BookPageBO::new);
    }

	@Override
	public List<String> batchCreate(List<BookPageBO> list) {
		List<String> ids = new ArrayList<String>();
		for (BookPageBO bookPageBO : list) {
			bookPageBO.setBookPageId(IdGen.uuid());
			bookPageBO.setCrateTime(new Date());
			ids.add(bookPageBO.getBookPageId());
		}
		List<BookPageBO> l = bookPageDao.insert(list);
		if(l.size()==list.size()) {
			//加入书本页码列表
			Optional<Book> optional =	bookDao.findById(list.get(0).getBookId());
	        if (!optional.isPresent()) {
	            throw new AppException("不存在的实例");
	        }
	        Book book =   optional.get();
		    if(book.getPageList()!=null) {
		    	List<BookPage> bpList = book.getPageList();
		    	bpList.addAll(l);
		    	bookDao.save(book);
		    }
			return ids;
		}
		return null;
	}
}
